<template>
  <div>
    <MyTable :list="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="scope">
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <input
            ref="inpt"
            class="tag-input form-control"
            style="width: 100px"
            type="text"
            v-if="scope.row.inputVisible"
            v-model="scope.row.inputValue"
            @keyup.esc="scope.row.inputValue = ''"
            @keyup.enter="addTags(scope.row)"
            @blur="scope.row.inputVisible = false"
          />
          <button
            v-else
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="btn(scope.row.id)"
          >
            +Tag
          </button>
          <span
            class="badge bg-warning text-dark"
            style="margin-right: 5px"
            v-for="(item, index) in scope.row.tags"
            :key="index"
          >
            {{ item }}</span
          >
        </td>
        <td>
          <button
            class="btn btn-danger btn-sm"
            @click="removeBtn(scope.row.id)"
          >
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from '../components/MyTable.vue'
export default {
  name: 'Day06Mygoodslist',

  data() {
    return {
      list: []
    }
  },
  components: { MyTable },
  async created() {
    let res = await this.$axios({
      url: '/api/goods'
    })
    console.log(res.data.data)
    this.list = res.data.data
  },

  methods: {
    removeBtn(id) {
      this.list = this.list.filter((item) => {
        return item.id !== id
      })
    },
    async btn(id) {
      this.list.forEach((item) => {
        if (item.id === id) {
          item.inputVisible = true
        }
      })
      await this.$nextTick()
      this.$refs.inpt.focus()
    },
    addTags(obj) {
      if (obj.inputValue.length === 0) alert('请输入内容!')
      obj.tags.push(obj.inputValue)
      obj.inputValue = ''
    }
  }
}
</script>

<style lang="scss" scoped></style>
